<template>
  <div class="login">
    <div class="bgc"></div>
    <div class="login-container-all">
      <div class="login-logo">
        <img src="../../assets/login_logo.png"/>
      </div>
      <div class="login-container">
        <div class="title">
          <div>杜氏肌营养不良基因突变分析软件</div>
          <div class="sub-title">DMD-1</div>
        </div>
        <a-form :form="form" @submit="handleSubmit" >
          <a-form-item>
            <a-input
              v-decorator="[
                'userCode',
                { rules: [{ required: true, message: $t('please_input') + $t('user_code') + $t('exclamation_mark') }] }
              ]"
              :placeholder="$t('user_code')"
              size="large"
            >
              <a-icon slot="prefix" type="user" style="color: rgba(0,0,0,.25)" />
            </a-input>
          </a-form-item>
          <a-form-item>
            <a-input-password
              v-decorator="[
                'password',
                { rules: [{ required: true, message: $t('please_input') + $t('password') + $t('exclamation_mark') }] }
              ]"
              type="password"
              :placeholder="$t('password')"
              size="large"
            >
              <a-icon slot="prefix" type="lock" style="color: rgba(0,0,0,.25)" />
            </a-input-password>
          </a-form-item>
          <a-form-item>
            <a-button type="primary" html-type="submit" class="login-form-button" size="large" :disabled="loginBtnDisabled" block >
              {{ $t('login') }}
            </a-button>
          </a-form-item>
        </a-form>
      </div>
      <div class="copyright">
        <div class="text">赛福基因版权所有</div>
      </div>
      <PasswordChange :displayLink="false" ref="passwordChange" />
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import qs from 'qs'
import md5 from 'md5'
export default {
  data () {
    return {
      form: this.$form.createForm(this, {name: this._uid}),
      loginBtnDisabled: false,
    }
  },
  methods: {
    handleSubmit (e) {
      e.preventDefault();
      this.form.validateFields((err, values) => {
        if (err) {
          return;
        }
        values.password = md5(values.password)

        this.loginBtnDisabled = true
        // 登录
        axios({
          method: 'post',
          url: '/infra/security/login',
          data: qs.stringify(values),
        }).then(res => {
          let authToken = res.data.authToken
          localStorage.setItem('authToken', authToken)
          axios.defaults.headers.common['Authorization'] = authToken

          if (res.data.forceChangePwd) {
            this.$refs.passwordChange.showModal()
            this.$store.dispatch('loadRsaPublicKey')
          } else {
            window.location.href = '/'
          }
          
        }).finally(()=>{
          this.loginBtnDisabled = false
        })
      });
    },
  },
};
</script>
<style>
.login {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 570px;
  height: 100vh;
}
.login .bgc{
  background: url('../../assets/login_bg.png');
  position:fixed;
  top: 0;
  left: 0;
  width:100%;
  height:100%;
  min-width: 1000px;
  z-index:-10;
  zoom: 1;
  background-color: #fff;
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center 0;
}
.login-container-all{
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100%;
  justify-content: space-between;
}
.login .title {
  flex: auto;
  letter-spacing: 2px;
  text-align: center;
  color: #fff;
  font-size: 25px;
}
.login .title .sub-title{
  font-size: 20px;
  margin-bottom: 15px;
}
.login .copyright {
  flex: auto;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  width: 100%;
  height: 100px;
}
.login .copyright .text {
  width: 100%;
  letter-spacing: 2px;
  text-align: center;
  font-weight: bold;
  color: #0188afc7;
  font-size: 24px;
  height: 96px;
}
.login .login-container {
  background: #0000001f;
  padding: 30px 60px;
  border-radius: 8px;
  position: relative;
  margin-top: 120px;
}
.login .login-container .text {
  font-size: 28px;
  font-weight: bold;
  text-align: center;
  color: #008c8c;
  margin-bottom: 16px;
}
.login .login-container .ant-form {
  width: 300px;
  margin: auto;
}
.login .login-logo img {
  width: 300px;
  margin-top: 140px;
}
</style>